from typing import Optional, Any
from typing import List
from langchain.callbacks.manager import CallbackManagerForLLMRun
from langchain.llms.base import LLM
from langchain.llms.utils import enforce_stop_tokens
from transformers import AutoModel, AutoTokenizer


class ChatGLMService(LLM):
    max_token: int = 2048
    temperature: float = 0.95
    top_p = 0.7
    history = []
    tokenizer: object = None
    model: object = None

    def __init__(self):
        super().__init__()

    @property
    def _llm_type(self) -> str:
        return "ChatGLM 6B int4"

    def _call(self, prompt: str, stop: Optional[List[str]] = None,
              run_manager: Optional[CallbackManagerForLLMRun] = None, **kwargs: Any) -> str:
        response, _ = self.model.chat(
            self.tokenizer,
            prompt,
            history=self.history,
            max_length=self.max_token,
            temperature=self.temperature,
        )
        if stop is not None:
            response = enforce_stop_tokens(response, stop)
        self.history = self.history + [[None, response]]
        return response

    def load_model(self, model_name_or_path: str, isqiamtize4, device):

        self.tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, trust_remote_code=True)
        if isqiamtize4:
            self.model = AutoModel.from_pretrained(model_name_or_path, trust_remote_code=True,
                                                   device=device).half().quantize(4)
        else:
            self.model = AutoModel.from_pretrained(model_name_or_path, trust_remote_code=True, device_map="auto")
        self.model = self.model.eval()


if __name__ == '__main__':
    service = ChatGLMService()
    service.load_model(model_name_or_path="E:\Project\Graph_LLM\params\chatglm3-6b", isqiamtize4=False, device="auto")
    tokenizer = AutoTokenizer.from_pretrained("E:\Project\Graph_LLM\params\chatglm3-6b", trust_remote_code=True)
    while True:
        user_input = input()
        response, history = service.model.chat(tokenizer, user_input, history=[])
        print(response)

